package com.twu.biblioteca.command;

import com.twu.biblioteca.Library;
import com.twu.biblioteca.Movie;

/**
 * Created with IntelliJ IDEA.
 * User: twer
 * Date: 5/30/13
 * Time: 12:30 AM
 * To change this template use File | Settings | File Templates.
 */
public class ViewAllMoviesCommand implements Command {

    private Library library;

    public ViewAllMoviesCommand(Library library){
        this.library = library;
    }

    @Override
    public String execute() {
        StringBuilder sb = new StringBuilder();
        sb.append("\t").append(String.format("%1$-15s%2$-6s%3$-10s%4$-3s","Movie", "Year", "Director", "Rating")).append("\n");
        for(Movie movie : library.getMovies()){
            sb.append("\t").append(formatMovieString(movie)).append("\n");
        }
        return sb.toString();
    }

    private String formatMovieString(Movie movie){
        return String.format("%1$-15s%2$-6s%3$-10s%4$-3s", truncate(movie.getName(), 13), movie.getYear(), truncate(movie.getDirector(), 8), movie.getRating());
    }

    private String truncate(String value, int length) {
        if (value != null && value.length() > length)
            value = value.substring(0, length - 3) + "...";
        return value;
    }
}
